using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using OneSoft.EntityCore;
using OneSoft.DataAccess;
using BySoft.Server.Common.Entity;
using BySoft.Server.Common.Schema;

namespace BySoft.Server.DataAccess
{
	public partial class MdAttachmentDA
	{
		#region GetByID

		/// <summary>
		/// 取得实体
		/// </summary>
		/// <param name="id">实体关键字</param>
		/// <returns></returns>
		public static MdAttachmentEntity GetMdAttachmentByID(String id)
		{
			string strSQL = "SELECT * FROM " + MdAttachmentTable.C_TABLENAME;
			WhereBuilder wb = new WhereBuilder(strSQL);
			wb.AddAndCondition(MdAttachmentTable.C_ATTACHMENTID, SQLOperator.Equal, id);
			return OneSoft.DataAccess.DataAccessCommon.SelectSingle<MdAttachmentEntity>(wb);
		}

		#endregion

		#region GetAll

		/// <summary>
		/// 获取所有数据
		/// </summary>
		/// <returns>所有数据集合</returns>
		public static DataSet GetMdAttachmentAll()
		{
			string strSQL = "SELECT * FROM " + MdAttachmentTable.C_TABLENAME;
			return OneSoft.DataAccess.DataAccessCommon.SelectDataSet(strSQL);
		}

		#endregion

		#region GetByCondition

		/// <summary>
		/// 根据Condition获取DS
		/// </summary>
		/// <returns>所有数据集合</returns>
		public static DataSet GetDSByCondition(string strCondition)
		{
			string strSQL = "SELECT * FROM " + MdAttachmentTable.C_TABLENAME;
			WhereBuilder wb = new WhereBuilder(strSQL);
			wb.AddCondition(strCondition);
			return OneSoft.DataAccess.DataAccessCommon.SelectDataSet(wb);
		}

		/// <summary>
		/// 根据Condition获取实体集合
		/// </summary>
		/// <param name="strCondition">SQL条件</param>
		/// <returns>实体集合</returns>
		public static List<MdAttachmentEntity> GetByCondition(string strCondition)
		{
			string strSQL = "SELECT * FROM " + MdAttachmentTable.C_TABLENAME;
			WhereBuilder wb = new WhereBuilder(strSQL);
			wb.AddCondition(strCondition);
			return OneSoft.DataAccess.DataAccessCommon.Select<MdAttachmentEntity>(wb);
		}

		#endregion
        private static string GetHeadSQL()
        {
            return @"SELECT A.*,a.CustomerID AS MatCustomerID, a.CustomerName AS MatCustomerName
                    FROM MD_Customer A 
                    WHERE 1=1";
        }
        public static DataTable SelectList(string strCustomerID, string strCustomerName, string strCustomerScaler, int pageIndex, int pageSize, out int count)
        {
            try
            {
                #region SQL Server
                string strcmd = GetHeadSQL();

                DataAccessParameterCollection dpc = new DataAccessParameterCollection();

                if (!string.IsNullOrEmpty(strCustomerID))
                {
                    strcmd += " and A.CustomerID LIKE @CustomerID";
                    dpc.AddWithValue("@CustomerID", "%" + strCustomerID + "%");
                }

                if (!string.IsNullOrEmpty(strCustomerName))
                {
                    strcmd += " and A.CustomerName LIKE @CustomerName";
                    dpc.AddWithValue("@CustomerName", "%" + strCustomerName + "%");
                }
                if (!string.IsNullOrEmpty(strCustomerScaler))
                {
                    strcmd += " and A.CustomerScaler=@CustomerScaler";
                    dpc.AddWithValue("@CustomerScaler", strCustomerScaler);
                }


                strcmd += " ORDER BY A.ModifyDate DESC";

                return DataAccessCommon.ExecuteDataTablePaging(strcmd, pageIndex, pageSize, dpc, out count);
                #endregion
            }
            catch
            {
                throw;
            }
        }

        public static DataTable GetMdAttachment(string BOMHalfID, string type)
        {
            try
            {
                #region SQL Server
                string strcmd = @"select * from MD_Attachment where 1=1 ";
                DataAccessParameterCollection dpc = new DataAccessParameterCollection();

                if (!string.IsNullOrEmpty(type))
                {
                    strcmd += " and FormType=@FormType";
                    dpc.AddWithValue("@FormType", type);
                }
                if (!string.IsNullOrEmpty(BOMHalfID))
                {
                    strcmd += " and FormID=@BOMHalfID";
                    dpc.AddWithValue("@BOMHalfID", BOMHalfID);
                }


                strcmd += " ORDER BY AttachmentID DESC";

                return DataAccessCommon.Select(strcmd, dpc).Tables[0];
                #endregion
            }
            catch
            {
                throw;
            }
        }
    }
}

